<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input placeholder="请输入内容" v-model="query">
            <el-button slot="append" @click="serch" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addRole">添加角色</el-button>
        </el-col>
      </el-row>
      <el-table
          :data="tableData"
          stripe
          border
          style="width: 100%">
        <el-table-column
            fixed
            prop="roleId"
            label="id"
            width="50"
        >
        </el-table-column>
        <el-table-column
            prop="roleName"
            label="角色名称"
        >
        </el-table-column>
        <el-table-column
            prop="status"
            label="状态">
          <template slot-scope="scope">
            <el-switch
                v-model="scope.row.status"
                active-value="0"
                inactive-value="1"
                active-color="#13ce66"
                @change="changeStatus(scope.row.roleId,scope.row.status)"
                inactive-color="#ff4949">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
            prop="createBy"
            label="创建人"
            >
        </el-table-column>
        <el-table-column
            prop="createTime"
            label="创建时间"
           >
        </el-table-column>
        <el-table-column
            prop="remark"
            label="角色介绍"
        >
        </el-table-column>
        <el-table-column
            fixed="right"
            label="操作"
            align="center"
            width="160">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" @click="EditRole(scope.row)"  size="medium" circle></el-button>
            <el-button type="danger" icon="el-icon-delete" @click="DeleteRole(scope.row)"  size="medium" circle></el-button>
            <el-button type="success" icon="el-icon-s-tools" size="medium" @click="FenMenus(scope.row)"  circle></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>


      <!--修改角色弹框的 表单 -->
      <el-dialog
          title="修改角色信息"
          :visible.sync="EditRoleDialogVisible"
          width="50%"
          @close="closeEditRole"
      >
        <el-form :model="EditRoleFormData" status-icon :rules="EditRoleFormRules" ref="EditRuleForm" label-width="80px" class="demo-ruleForm">

          <el-form-item label="角色名称" prop="roleName">
            <el-input type="text" v-model="EditRoleFormData.roleName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="角色介绍" prop="remark">
            <el-input type="text"  v-model="EditRoleFormData.remark" autocomplete="off"></el-input>
          </el-form-item>

        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="EditRoleDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirmEditRole">确 定</el-button>
        </span>
      </el-dialog>


      <!--添加弹框的 表单 -->
      <el-dialog
          title="添加用户信息"
          :visible.sync="AddRoleDialogVisible"
          width="50%"
          @close="closeAddRole"
      >
        <el-form :model="AddRoleFormData" status-icon :rules="AddRoleFormRules" ref="AddRuleForm" label-width="80px" class="demo-ruleForm">

          <el-form-item label="角色名称" prop="roleName">
            <el-input type="text" v-model="AddRoleFormData.roleName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="角色介绍" prop="remark">
            <el-input type="text"  v-model="AddRoleFormData.remark" autocomplete="off"></el-input>
          </el-form-item>

        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="AddRoleDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirmAddRole">确 定</el-button>
        </span>
      </el-dialog>


      <!--分配菜单的弹框 -->
      <el-dialog
          :title="title"
          :visible.sync="FenMenuDialogVisible"
          width="50%"
      >

        <el-tree
            :data="MenuData"
            show-checkbox
            ref="MenuTree"
            node-key="menuId"
            :default-expanded-keys="defaultExpandedKeys"
            :default-checked-keys="defaultCheckedKeys"
            :props="defaultProps">
        </el-tree>

        <span slot="footer" class="dialog-footer">
          <el-button @click="FenMenuDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirmFenMenu">确 定</el-button>
        </span>
      </el-dialog>


    </el-card>
  </div>
</template>

<script>
export default {
  name: "role",
  data(){
    return{
      //查询条件
      query:"",
      //当前页码
      currentPage: 1,
      //每页显示记录
      pageSize:5,
      //总条数
      total:0,
      //当前的记录数据
      tableData: [],
      //是否打开修改弹框 true 打开 false 关闭
      EditRoleDialogVisible: false,
      //修改弹框表单的数据
      EditRoleFormData:{},
      //修改弹框 表单的 规则
      EditRoleFormRules:{
        roleName:[
          {required:true,message:"角色名称名不能为空",trigger:"blur"}
        ],
        remark:[
          {required:true,message:"角色介绍不能为空",trigger:"blur"},
        ]
      },
      //是否打开 添加弹框
      AddRoleDialogVisible: false,
      //添加表单的数据
      AddRoleFormData:{},
      //添加表单的规则
      AddRoleFormRules:{
        roleName:[
          {required:true,message:"角色名称名不能为空",trigger:"blur"}
        ],
        remark:[
          {required:true,message:"角色介绍不能为空",trigger:"blur"},
        ]
      },
      //是否打开分配弹框
      FenMenuDialogVisible: false,
      //为那个用户分配 菜单
      title:"分配菜单信息",
      roleId:0,

      MenuData: [],
      defaultProps: {
        children: 'children',
        label: 'menuName'
      },
      defaultExpandedKeys:[],
      defaultCheckedKeys:[],
      //选中的节点
      checkedMenuNods:[]

    }
  },
  //页面加载自动执行
  created() {
    this.initUserTable(this.currentPage,this.pageSize);
  },
  methods: {
    //修改每页几条的方法
    handleSizeChange(val) {
      this.pageSize=val;
      this.initUserTable(this.currentPage,this.pageSize);
    },
    //修改页数的方法
    handleCurrentChange(val) {
      this.currentPage=val;
      this.initUserTable(this.currentPage,this.pageSize);
    },
    //模糊查询按钮的方法
    serch(){
      this.initUserTable(this.currentPage,this.pageSize);
    },
    //初始化表格的方法
    initUserTable(currentPage,pageSize){
      var that = this;
      this.$http.post(`/sys/role/findRoleByPage/${currentPage}/${pageSize}?query=${this.query}`).then(function (res){
        if (res.data.code==200){
          that.tableData=res.data.data.records;
          that.total=res.data.data.total;
        }else{
          that.$message.error("用户信息加载失败");
        }
      })
    },
    //修改角色弹框回显的方法
    EditRole(row){
      this.EditRoleDialogVisible = true;
      this.EditRoleFormData = row;
    },
    //提交修改弹框的方法
    confirmEditRole(){
      var that = this;
      this.$refs['EditRuleForm'].validate((valid) => {
        if (valid) {
          that.$http.put("/sys/role/updateRole",this.EditRoleFormData).then(function (res){
            if (res.data.code == 200){
              that.$message.success(res.data.msg);
              that.EditRoleDialogVisible=false;
              that.initUserTable(that.currentPage,that.pageSize);
            }else{
              that.$message.error(res.data.msg);
            }
          })
        }
      })
    },
    //修改 状态的方法
    changeStatus(roleId,status){
      var that = this;
      this.$http.put(`/sys/role/updateStatusByRoleId/${roleId}/${status}`).then(function (res){
        if (res.data.code == 200){
          that.$message.success(res.data.msg);
          that.initUserTable(that.currentPage,that.pageSize);
        }else{
          that.$message.error(res.data.msg);
        }
      })
    },
    //添加按钮的弹框的方法
    addRole(){
      this.AddRoleDialogVisible=true;
    },
    //提交添加表单的方法
    confirmAddRole(){
      //console.log(this.AddFormData);
      var that = this;
      this.$refs['AddRuleForm'].validate((valid) => {
        if (valid) {
          this.$http.post("/sys/role/addRole", this.AddRoleFormData).then(function (res) {
            if (res.data.code == 200) {
              that.$message.success(res.data.msg);
              that.AddRoleDialogVisible = false;
              that.initUserTable(that.currentPage, that.pageSize);
            } else {
              that.$message.error(res.data.msg);
            }
          })
        }
      })
    },
    //打开分配弹框 并回显数据的 方法
    FenMenus(row){
      var that = this;
      this.roleId = row.roleId;
      this.title = "为"+row.roleName+"分配菜单"
      this.$http.get("/sys/menu/getAllMenus/"+row.roleId).then(function (res){
        if (res.data.code ==200){
          that.MenuData=res.data.data.menus;
          that.defaultExpandedKeys=res.data.data.roleMenus;
          //that.defaultCheckedKeys=res.data.data.roleMenus;
          setTimeout(()=>{
            res.data.data.roleMenus.forEach(value=>{
              that.$refs.MenuTree.setChecked(value,true,false);
            })
          })
          that.FenMenuDialogVisible = true;
        }
      })

    },
    //提交分配权限的弹出框
    confirmFenMenu(){
      var that =this;
      //this.$refs.MenuTree.getCheckedKeys() 获取到选中的 节点的id
      //console.log(this.$refs.MenuTree.getCheckedKeys());
      //console.log(this)
      this.checkedMenuNods = this.$refs.MenuTree.getCheckedKeys().concat(this.$refs.MenuTree.getHalfCheckedKeys());
      this.$http.post("/sys/menu/confirmFenMenu",{roleId:this.roleId,menuIds:this.checkedMenuNods}).then(function (res){
        if (res.data.code==200){
          that.$message.success(res.data.msg);
          that.FenMenuDialogVisible = false;
        }else {
          that.$message.error("修改用户角色失败")
        }
      })
    },
    //修改弹框关闭时触发的方法
    closeEditRole(){
      this.$refs.EditRuleForm.resetFields();
    },
    //添加弹框关闭时触发的方法
    closeAddRole(){
      this.$refs.AddRuleForm.resetFields();
    },
    //删除角色信息
    DeleteRole(row){
      var that=this;
      this.$confirm('是否删除用户:'+row.roleName+'?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        that.$http.get(`/sys/role/deleteRoleById/${row.roleId}`).then(function(res){
          if (res.data.code == 200){
            if (that.total%that.pageSize==1){
              that.currentPage--;
            }else {
              that.currentPage = that.currentPage;
            }
            that.$message.success(res.data.msg);
            that.initUserTable(that.currentPage,that.pageSize);
          }else {
            that.$message.error(res.data.msg);
          }
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }

  }
}
</script>

<style scoped>

</style>